<?php
error_reporting(1);
if (empty($_COOKIE['_c_chk']))
{
    //header('Location: login.php');
    exit("<script>top.document.location.href='login.php'</script>");
}
$_c_chk = htmlspecialchars($_COOKIE['_c_chk'], ENT_QUOTES);
if (strlen(base64_decode(substr($_c_chk, 4))) != 10)
{
    unset($_COOKIE['_c_chk']);
    //header('Location: login.php');
    exit("<script>top.document.location.href='login.php'</script>");
}

$g_conf = array ();
include_once("hip.php");
if (!is_array($g_betname))
{
    echo "<font color=red>不可预期的错误(1)，服务终止！</font>";
    exit();
}
include_once("function.php");
$blackList = "#inc/blackip.php";
if (file_exists($blackList))
{
    include_once( $blackList );
    $chkip = getIP();
    if (in_array($chkip, $g_blackipArr))
    {
        echo "HTTP/1.0 404 Not Found";
        exit();
    }
}
include_once( "class/functions.php" );
include_once( "class/user.php" );
if (user::islogin_cookie() == FALSE)
{
    unset($_COOKIE['_c_chk']);
    //header('Location: login.php');
    exit("<script>top.document.location.href='login.php'</script>");
    exit();
}
define("IN_OP", 1);
include_once( "config.php" );
include_once( "class/db.php" );
include_once( "class/sessions.php" );
include_once( "class/tmd_tpl.php" );
include_once( "class/template.php" );
include_once( "class/ui.php" );
include_once( "class/config.php" );
include_once( "class/class.tree.php" );

session_cache_expire(20);
define("G_SCRIPTSTART", getmicrotime());
$op = getvar("op");
$fp = getvar("fp");
$conf = new Config();
$user = new User();
$curruser = $user->GetUser();
$gtype = intval(getvar("gametype"));
if (!isset($g_menu_enabled[$gtype]) || $g_menu_enabled[$gtype] == 0)
{
    exit($gtype . " error No");
}
ob_start();
$g_conf['language'] = "zh-cn";
if (isset($_COOKIE['jslang']) && $_COOKIE['jslang'] == 0)
{
    $g_conf['language'] = "zh-tw";
}
else
{
    $g_conf['language'] = "zh-cn";
}
setcookie("g_language", $g_conf['language'], systime() + 24 * 60 * 60);
if (file_exists("lang/zh-tw.php"))
{
    include_once( "lang/zh-tw.php" );
}
$g_addTime = intval($conf->get("ServerAdjustTime"));
if ($user->IsLogin() == false)
{
    $op = "core";
    $fp = "login";
}
else
{
    $sys_init_start_mode = $conf->get("init_start_mode");
    $g_init_mode_6h = true;
    $g_init_mode_3d = true;
    switch ($sys_init_start_mode)
    {
        case 1:
            switch ($curruser['acl'])
            {
                case "255" :
                    $init_start_mode = 1;
                    break;
                case "5" :
                    $init_start_mode = $curruser['init_start_mode'];
                    break;
                default :
                    $subsidaryInfo = get_user(get_subsidary_id($curruser['id']));
                    $init_start_mode = $subsidaryInfo['init_start_mode'];
                    break;
            }
            if ($init_start_mode == 2)
            {
                $g_init_mode_3d = false;
            }
            else if ($init_start_mode == 3)
            {
                $g_init_mode_6h = false;
            }
            break;
        case 2 :
            $g_only_6h = " style='display:none;' ";
            break;
        case 3 :
            $g_init_mode_6h = false;
            $g_only_6h = "";
            break;
    }
    $allSystemcheck = false;
    if ($fp == "")
    {
        if ($g_init_mode_6h)
        {
            $fp = "index";
            $idxGametype = 0;
        }
        else
        {
            $fp = "index_3d";
            $idxGametype = 1;
        }
        if ($conf->get("systemcheck_6h") != 1 && $g_init_mode_6h && $g_menu_enabled[0])
        {
            $fp = "index";
            $idxGametype = 0;
        }
        else if ($conf->get("systemcheck_3d") != 1 && $g_init_mode_3d && $g_menu_enabled[1])
        {
            $fp = "index_3d";
            $idxGametype = 1;
        }
        else if ($conf->get("systemcheck_3t") != 1 && $g_init_mode_3d && $g_menu_enabled[2])
        {
            $fp = "index_3d";
            $idxGametype = 2;
        }
        else if ($conf->get("systemcheck_3s1") != 1 && $g_init_mode_3d && $g_menu_enabled[3])
        {
            $fp = "index_3d";
            $idxGametype = 3;
        }
        else
        {
            $fp = "index_3d";
            $idxGametype = 10;
            if ($conf->get("systemcheck_3s3") != 1 && $g_init_mode_3d && $g_menu_enabled[12])
            {
                $fp = "index_3d";
                $idxGametype = 12;
            }
            else if ($conf->get("systemcheck_3s4") != 1 && $g_init_mode_3d && $g_menu_enabled[13])
            {
                $fp = "index_3d";
                $idxGametype = 13;
            }
            else if ($conf->get("systemcheck_5h") != 1 && $g_init_mode_3d && $g_menu_enabled[20])
            {
                $fp = "index_3d";
                $idxGametype = 20;
            }
            else if ($conf->get("systemcheck_8h") != 1 && $g_init_mode_3d && $g_menu_enabled[21])
            {
                $fp = "index_3d";
                $idxGametype = 21;
            }
            else if ($curruser['acl'] != 255)
            {
                $allSystemcheck = true;
            }
        }
    }
	
    if ($allSystemcheck)
    {
        diemsg("系统维护中");
        exit();
    }
    if ($curruser['enabled'] == 0 || 0 < $curruser['disabled'])
    {
        if ($op == "core" && $fp == "logout")
        {
            include_once( "{$op}/{$fp}.php" );
            exit();
        }
        $user->logout();
        if ($curruser['enabled'] == 0)
        {
            diemsg(L_ACCOUNTDISABLE);
        }
        else
        {
            header("Location:http://cn.yahoo.com/");
            exit();
        }
    }
	
    if ($op == "core" && $fp == "logout")
    {
        include_once( "{$op}/{$fp}.php" );
        exit();
    }
    if ($op == "")
    {
        switch ($curruser['acl'])
        {
            case 1 :
                $op = "member";
                break;
            case 2 :
                $op = "agent";
                break;
            case 3 :
                $op = "genagent";
                break;
            case 4 :
                $op = "shareowner";
                break;
            case 5 :
                $op = "subsidary";
                break;
            case 255 :
                $op = "admin";
                break;
            case 300 :
                $op = "centralize";
                break;
        }
    }

    if ($op != "core" && $conf->get("systemcheck") == 1 && $curruser['acl'] != 255)
    {
        redirect("op.php?op=core&fp=systemcheck");
        exit();
    }
    if (!$curruser['chgpwdt'] && !isset($_SESSION['subaccount']))
    {
        $op = "core";
        $fp = "chgpw";
    }
    $user->setLogin_cookie();
}
if (file_exists($op . "/lang/zh-tw.php"))
{
    include_once( $op . "/lang/zh-tw.php" );
}

$weekname = array (L_SUNDAY, L_MONDAY, L_TUESDAY, L_WEDNESDAY, L_THURSDAY, L_FRIDAY, L_SATURDAY);
$aclname = array ("-1" => "连络人", "1" => L_MEMBER, "2" => L_AGENT, "3" => L_GENAGENT, "4" => L_SHAREOWNER, "5" => "大股东", "255" => L_ADMIN, "300" => "连控操盘员");
$g_chinayearnum = GetChinaYearNumber();
$g_chinayearnumpic = GetChinaYearNumberPic();
$g_chinayearnumpic6 = GetChinaYearNumberPic6();
$g_red_po = array (1, 2, 7, 8, 12, 13, 18, 19, 23, 24, 29, 30, 34, 35, 40, 45, 46);
$g_blue_po = array (3, 4, 9, 10, 14, 15, 20, 25, 26, 31, 36, 37, 41, 42, 47, 48);
$g_green_po = array (5, 6, 11, 16, 17, 21, 22, 27, 28, 32, 33, 38, 39, 43, 44, 49);
$g_sumnum_odd = array (1, 3, 5, 7, 9, 10, 12, 14, 16, 18, 21, 23, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 45, 47);
$g_sumnum_even = array (2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, 28, 31, 33, 35, 37, 39, 40, 42, 44, 46, 48);
$g_sumnum_dom = Get_DomNumber($g_chinayearnum);
$g_sumnum_wild = Get_WildNumber($g_chinayearnum);
$g_chinese_t = array ("0" => "零", "1" => "一", "2" => "二", "3" => "三", "4" => "四", "5" => "五", "6" => "六", "7" => "七", "8" => "八", "9" => "九");
$g_chinese_m = array ("0" => "０", "1" => "１", "2" => "２", "3" => "３", "4" => "４", "5" => "５", "6" => "６", "7" => "７", "8" => "８", "9" => "９");
$path = "{$op}/{$fp}.php";
if (file_exists($path))
{
    if ($op != "core")
    {
        switch ($op)
        {
            case "member" :
            case "member_3d" :
            case "member_5h" :
                if ($curruser['acl'] != 1)
                {
                    diemsg(L_NOTMEMBER);
                }
                break;
            case "agent" :
                if ($curruser['acl'] < 2)
                {
                    diemsg(L_NOTAGENT);
                }
                break;
            case "genagent" :
                if ($curruser['acl'] < 3)
                {
                    diemsg(L_NOTGENAGENT);
                }
                break;
            case "shareowner" :
                if ($curruser['acl'] < 4)
                {
                    diemsg(L_NOTSHAREOWNER);
                }
                break;
            case "subsidary" :
                if ($curruser['acl'] < 5)
                {
                    diemsg("你不是大股东！");
                }
                break;
            case "admin" :
                if ($curruser['acl'] < 255)
                {
                    diemsg(L_NOTADMIN);
                }
                break;
            case "centralize" :
                if ($curruser['acl'] < 300)
                {
                    diemsg("你不是连控操盘者！");
                }
                break;
        }
    }
    include_once( $path );
}
else
{
    diemsg(L_NOFOUNDMOD . $op . "::" . $fp);
}
$str = ob_get_contents();
ob_end_clean();
echo $str;